﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MossmanMill.DataModel;
using MossmanMill.Properties;

namespace MossmanMill
{
    public partial class InventoryManager : Form
    {
        private List<Card> Cards;
        private bool loaded;

        public InventoryManager()
        {
            InitializeComponent();
        }

        private void InventoryManager_Load(object sender, EventArgs e)
        {
            if (loaded) return;
            loaded = true;
            var db = DatabaseAccess.Instance;
            Cards = db.GetAvailableCards();
            ModuleList.Items.Clear();
            foreach (Card card in Cards) ModuleList.Items.Add(card);
        }

        private void OnModuleSelect(object sender, EventArgs e)
        {
            var card = (Card)ModuleList.SelectedItem;
            CardInformation.Lines = card.GetInfo();
        }

        private void AddNewModule(object sender, EventArgs e)
        {
            var model = NewCardModel.Text;
            if (String.IsNullOrEmpty(model) || NewCardType.SelectedIndex < 0 || NewCardChannels.SelectedIndex < 0)
            {
                MessageBox.Show(Resources.FormValidationMsg);
                return;
            }
            var type = NewCardType.SelectedIndex;
            var channels = Int32.Parse(NewCardChannels.SelectedItem.ToString());
            var desc = NewCardDesc.Text;
            var db = DatabaseAccess.Instance;
            db.AddIOModule(model, type, channels, desc);
            loaded = false;
            InventoryManager_Load(null, null);
            NewCardModel.Text = "";
            NewCardType.SelectedIndex = -1;
            NewCardChannels.SelectedIndex = -1;
            NewCardDesc.Text = "";
        }
    }
}
